package cn.liziguo.tank.factory;

import cn.liziguo.tank.config.GameConfig;
import cn.liziguo.tank.constants.GameEnum;
import cn.liziguo.tank.constants.ImageConst;
import cn.liziguo.tank.script.HpBarComponent;
import cn.liziguo.tank.script.enemy.EnemyComponent;
import cn.liziguo.tank.script.player.PlayerComponent;
import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.dsl.components.HealthIntComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.EntityFactory;
import com.almasb.fxgl.entity.components.CollidableComponent;
import com.almasb.fxgl.ui.ProgressBar;
import javafx.geometry.Rectangle2D;
import javafx.scene.paint.Color;

/**
 * @author Liziguo
 * @date 2022/7/8 13:53
 */
public final class TankFactory implements EntityFactory {

    private static ProgressBar createHpBar(Entity owner, HealthIntComponent healthIntComponent) {
        ProgressBar hpBar = new ProgressBar(false);
        hpBar.setLabelVisible(false);
        hpBar.maxValueProperty().bind(healthIntComponent.maxValueProperty());
        hpBar.currentValueProperty().bind(healthIntComponent.valueProperty());
        final double offsetW = 20;
        final double offsetH = 20;
        hpBar.setWidth(owner.getWidth() + offsetW);
        hpBar.setLayoutX(-(owner.getWidth() + offsetW) / 2);
        hpBar.setTranslateY(-(owner.getHeight()) / 2 - offsetH);
        Entity entity = FXGL.entityBuilder()
                .type(GameEnum.HP_BAR)
                .view(hpBar)
                .with(new HpBarComponent(owner))
                .zIndex(9)
                .build();
        FXGL.getGameWorld().addEntity(entity);
        return hpBar;
    }

    private static void createPlayerHpBar(Entity owner, HealthIntComponent healthIntComponent) {
        ProgressBar hpBar = createHpBar(owner, healthIntComponent);
        hpBar.setFill(Color.ROYALBLUE);
    }

    private static void createEnemyHpBar(Entity owner, HealthIntComponent healthIntComponent) {
        ProgressBar hpBar = createHpBar(owner, healthIntComponent);
        hpBar.setFill(Color.INDIANRED);
    }

    public static Entity createPlayer() {
        HealthIntComponent healthIntComponent = new HealthIntComponent(GameConfig.PLAYER_MAX_HP);
        Entity entity = FXGL.entityBuilder()
                .type(GameEnum.PLAYER)
//                .bbox(BoundingShape.box(457,457))
                .viewWithBBox(ImageConst.PLAYER_1)
                .with(new CollidableComponent(true))
                .with(new EffectComponent())
                .with(healthIntComponent)
                .with(new PlayerComponent())
                .at(GameConfig.PLAYER_1_INIT_POINT)
                .build();
        // 设置玩家出生位置为 家的左边
        FXGL.getGameWorld().addEntity(entity);
        createPlayerHpBar(entity, healthIntComponent);
        return entity;
    }

    public static Entity createEnemy() {
        HealthIntComponent healthIntComponent = new HealthIntComponent(GameConfig.ENEMY_MAX_HP);
        Entity entity = FXGL.entityBuilder()
                .type(GameEnum.ENEMY)
                .at(FXGLMath.randomPoint(new Rectangle2D(0, 0, GameConfig.WIDTH, 0)))
                .viewWithBBox(FXGLMath.random(ImageConst.ENEMIES).orElseThrow())
                .collidable()
                .with(healthIntComponent)
                .with(new EffectComponent())
                .with(new EnemyComponent())
                .build();
        FXGL.getGameWorld().addEntity(entity);
        createEnemyHpBar(entity, healthIntComponent);
        return entity;
    }

}
